package com.example.lcpractice.lc;

import com.example.lcpractice.datastructure.TreeNode;

/**
 * 101. 对称二叉树
 * 简单
 * 2.2K
 * 相关企业
 * 给你一个二叉树的根节点 root ， 检查它是否轴对称。
 *
 *
 *
 * 示例 1：
 *
 *
 * 输入：root = [1,2,2,3,4,4,3]
 * 输出：true
 * 示例 2：
 *
 *
 * 输入：root = [1,2,2,null,3,null,3]
 * 输出：false
 *
 *
 * 提示：
 *
 * 树中节点数目在范围 [1, 1000] 内
 * -100 <= Node.val <= 100
 */
public class Lc101 {
    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);

        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(2);
        node1.left=node2;
        node1.right=node3;

        TreeNode node4 = new TreeNode(3);
        TreeNode node5 = new TreeNode(4);
        node2.left = node4;
        node2.right = node5;
//        TreeNode node5 = new TreeNode(3);
        TreeNode node6 = new TreeNode(3);
        node3.right = node6;

        Lc101 lc101 = new Lc101();
        System.out.println(lc101.isSymmetric(node1));

    }

    boolean isSym = true;
    public boolean isSymmetric(TreeNode root) {
        trace(root, root);
        return isSym;
    }

    private void trace(TreeNode left, TreeNode right) {
        if (onlyOneNull(left, right)) {
            return;
        }
        if(left!=null&&right!=null&&left.val!= right.val){
            isSym = false;
            return;
        }
        if(left!=null&&right!=null){
            trace(left.left,right.right);
            trace(left.right,right.left);
        }

    }

    private boolean onlyOneNull(TreeNode left, TreeNode right) {
        if(left ==null&& right !=null){
            isSym = false;
            return true;
        }
        if(right ==null&& left !=null){
            isSym = false;
            return true;
        }
        return false;
    }


}
